home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1468 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  70 lines

  1. Path: oxy.rust.net!usenet
  2. From: ebennett@rust.net
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Question: references to variables within classes
  5. Date: Thu, 11 Jan 1996 05:15:44 GMT
  6. Organization: Rust Net - High Speed Internet in Detroit  810-642-2276
  7. Message-ID: <4d1rq9$sla@oxy.rust.net>
  8. References: <DKy0MC.3nA@watserv3.uwaterloo.ca>
  9. NNTP-Posting-Host: liv-16.rust.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. sdbay@watnow.uwaterloo.ca (Stephen Bay) wrote:
  13.  
  14. >Hi,
  15.  
  16. >Is it possible in C++ to use variables within a class as parameters for a 
  17. >constructor of an object contained within that class? I've tried compiling the 
  18. >following code and my compiler chokes on the list iter declaration within the 
  19. >class dodo - it doesn't recognize CityList and gives a syntax error. All the 
  20. >other statements compile fine though.
  21.  
  22. >-stephen
  23.  
  24.  
  25. >#include "slist.hpp"
  26.  
  27. >NISList<int *>            CityList;
  28. >NISListIter<int *>        CityListIter(CityList);
  29.  
  30. >struct bird
  31. >{
  32. >    NISList<int *>            CityList;
  33. >    NISListIter<int *>        CityListIter();
  34. >};
  35.  
  36. >class    dodo
  37. >{
  38. >    public:
  39. >        NISList<int *>            CityList;
  40. >chokes here->    NISListIter<int *>        CityListIter(CityList);
  41. >};
  42.  
  43. >main()
  44. >{
  45. >    NISList<int *>            CityList;
  46. >    NISListIter<int *>        CityListIter(CityList);
  47. >};
  48.  
  49. You are trying to call the constructor for CityListIter within the
  50. class definition.  This is illegal.  You should be able to change this
  51. as follows:
  52.  
  53.    class dodo
  54.    {
  55.        public:
  56.            NISLit<int *>     CityList;
  57.           NISListIter<int *> CityListIter;
  58.  
  59.           dodo() : CityListIter(CityList) {};
  60.    };
  61.  
  62. Since templates are involved, and I am not up to speed on templates, I
  63. am not 100% sure this will work this way, but it is what I would try
  64. first.
  65.  
  66. Earl Bennett
  67.  
  68.  
  69.  
  70.